<?php


namespace App\Http\Resources\posts;


use App\Models\posts\Posts;
use Illuminate\Http\Resources\Json\Resource;

class PostsResource extends Resource
{
    public function __construct(Posts $resource)
    {
        $this->resource = $resource;
    }


    public function toArray($request){
        
        if ($this->resource->up->id && $this->resource->up->status == 1){
            $is_up = 1;
        }else{
            $is_up = 0;
        }

        if ($this->resource->collection->id && $this->resource->collection->status == 1){
            $is_collection = 1;
        }else{
            $is_collection = 0;
        }

        if ($this->resource->follow->id && $this->resource->follow->status == 1){
            $is_follow = 1;
        }else{
            $is_follow = 0;
        }

        return [
            'id' => $this->resource->id,
            'imgs' => $this->resource->imgs??[],
            'title' => $this->resource->title??'',
            'content' => $this->resource->content??'',
            'url' => $this->resource->url??'',
            'type' => $this->resource->type,
            'is_show' => $this->resource->is_show,
            'is_up' => $is_up,
            'is_follow' => $is_follow,
            'is_collection' => $is_collection,
            'posts_cate_id' => $this->resource->posts_cate_id,
            'posts_cate_name' => $this->resource->posts_cate->name??'',
            'goods_id' => $this->resource->goods_id,
            'goods_name' => $this->resource->goods->name??'',
            'share_num' => $this->resource->share_num,
            'up_num' => $this->resource->up_num,
            'comment_num' => $this->resource->comment_num,
            'user_id' => $this->resource->user_id,
            'user_name' => $this->resource->user->name,
            'user_sex' => $this->resource->user->sex,
            'user_header_img' => $this->resource->user->header_img,
            'brief' => $this->resource->user->brief??'',
            'school' => $this->resource->user->school??'',
            'bg' => $this->resource->user->bg??'',
            'is_system' => $this->resource->is_system,
            'created_at' => $this->resource->created_at->toDateTimeString()
        ];
    }
}